package me.send.data.service.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.data.service.mapper.TreeMapper;
import me.send.data.service.pojo.Tree;
import me.send.data.service.pojo.ZTree;
import me.send.framework.util.BasePath;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TreeService {

	@Autowired
	private TreeMapper treeMapper;
	@Autowired
	ManagerService managerService;

	/** 使用 控制页面的 权限的获取 2015年6月19日15:28:31 * */
	public String main_tree(Integer pid,HttpServletRequest request,String address) {
		
		String main_tree = "";
		// 查询一级菜单,pid=0
		List<Tree> treeList = treeMapper.listByPid(pid);
		// 判断数据库的List
		for (Tree tree : treeList) {
			Subject subject = SecurityUtils.getSubject();
			try {
				if (subject.isPermitted(tree.getPermission())) {
					// 增加单个
					String treeContext = "<a href='"+BasePath.getBasePath(request)
							+ tree.getUrl()
							+ "'><i class='fa fa-dashboard'></i><span style=\"font-family: '微软雅黑';\">"
							+ tree.getName() + "</span></a>";
					String chContext = "";
					List<Tree> list2 = treeMapper.listByPid(tree.getTid());
					// 判断 如果list2的大小大于0

					if (list2 != null && list2.size() > 0) {
						treeContext = "<a href='#'><i class='fa fa-dashboard'></i><span style=\"font-family: '微软雅黑';\">"
								+ tree.getName()
								+ "</span><i class='fa fa-angle-left pull-right'></i></a>";
						chContext = "<ul class='treeview-menu' style=\"font-family: '微软雅黑';\">";
						// 设置一个开关 
						boolean isFlag = false;
						for (Tree tree2 : list2) {
							
							// 判断2级页面的url
							if (address!=null && !"".equals(address) && tree2!=null && tree2.getUrl()!=null && address.equals(tree2.getUrl())) {
//							topString = "<li class='treeview active'>";
									chContext += "<li class='active'><a href='"+BasePath.getBasePath(request) + tree2.getUrl()
									+ "'><i class='fa fa-circle-o'></i> "
									+ tree2.getName() + " </a></li>";
									isFlag = true;
							}
							else {
//						topString = "<li class='treeview'>";
								chContext += "<li><a href='"+BasePath.getBasePath(request) + tree2.getUrl()
								+ "'><i class='fa fa-circle-o'></i> "
								+ tree2.getName() + " </a></li>";
							}
						}
						chContext += "</ul>";
						// 判断开关是否是true
						if (isFlag) {
							main_tree+="<li class='treeview active'>";
						}else{
							main_tree+="<li class='treeview'>";
						}
						main_tree += treeContext + chContext;
						
					}
					main_tree += "</li>";
				}
			} catch (Exception e) {
				continue;
			}		
		}
		return main_tree;
	}

	public List<ZTree> list(Integer pid) {
		// 首页查询根菜单树
		List<Tree> treeList = treeMapper.listByPid(pid);
		// 实例化一下 返回值 类型是ZTree
		List<ZTree> list = new ArrayList<ZTree>();
		// 判断数据库的List
		for (Tree tree : treeList) {
			// 实例化一下返回类型对象
			ZTree zTree = new ZTree();
			// set值
			zTree.setName(tree.getName());
			zTree.setClick("load_tree('" + tree.getUrl() + "','"
					+ tree.getName() + "')");
			// zTree.setUrl(tree.getTurl());
			// 调用本身的方法 查看List的大小
			List<ZTree> list2 = this.list(tree.getTid());
			if (list2 != null && list2.size() > 0) {
				zTree.setChildren(list2);
				zTree.setParent(true);
			}
			list.add(zTree);
		}
		return list;
	}

}
